package com.jali.crud.service;

import com.jali.crud.bean.JaTask;
import com.jali.crud.bean.JaTaskItem;
import com.jali.crud.dao.JaTaskItemMapper;
import com.jali.crud.dao.JaTaskMapper;
import com.jali.crud.dao.JaUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jali
 * @create 2019-01-21 21:43
 */
@Service
public class JaTaskItemService {

    @Autowired
    JaTaskItemMapper jaTaskItemMapper;

    @Autowired
    JaTaskMapper jaTaskMapper;

    @Autowired
    JaUserMapper jaUserMapper;

    public List<JaTaskItem> selectTaskItems(){
        return jaTaskItemMapper.selectTaskItems(null);
    }

    public List<JaTaskItem> getTaskItemsByParentId(int parentId){
        return jaTaskItemMapper.getTaskItemsByParentId(parentId);
    }

    public int insertSelective(JaTaskItem record){
        return jaTaskItemMapper.insertSelective(record);
    }

    public JaTaskItem getTaskItemById(int id){
        return jaTaskItemMapper.selectByPrimaryKey(id);
    }

    public void updateUseTime(JaTaskItem jaTaskItem){
        jaTaskItemMapper.updateUseTime(jaTaskItem);
        jaTaskItem = jaTaskItemMapper.selectByPrimaryKey(jaTaskItem.getId());
        int score = jaTaskItem.getScore();
        int count = jaTaskItemMapper.selectUnfinishCount(jaTaskItem.getParentid());
        if(count<=0){
            jaTaskMapper.updateStatusById(jaTaskItem.getParentid());
            JaTask jaTask = jaTaskMapper.selectByPrimaryKey(jaTaskItem.getParentid());
            score += jaTask.getScore();
        }
        jaUserMapper.updateScoreAddToUser(1,score);
    }

}
